//
// Created by Kai Wang on 2023/12/12.
//
#include "order/order_comment.h"
#include "item/item.h"
#include "order/order.h"

#include <spdlog/spdlog.h>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>

time_point OrderComment::get_comment_time() const
{
    return comment_time_;
}

void OrderComment::set_comment_time()
{
    set_comment_time(std::chrono::system_clock::now());
}

void OrderComment::set_comment_time(const time_point &comment_time)
{
    comment_time_ = comment_time;
}

void OrderComment::add_comment(Item &item, float mark, std::string &comment)
{
    item_comments_.emplace_back(item, mark, comment);
}

std::vector<ItemComment> &OrderComment::get_item_comments()
{
    return item_comments_;
}

std::string &OrderComment::get_order_comment()
{
    return order_comment_;
}

void OrderComment::set_order_comment(
    std::string              &order_comment,  //
    std::vector<Item>        &items,          //
    std::vector<std::string> &item_comments,  //
    std::vector<float>        marks           //
)
{
    order_comment_ = order_comment;
    for(auto i = 0; i < items.size(); i++)
    {
        add_comment(items[i], marks[i], item_comments[i]);
    }
}

void Convert::String2OrderComment(const OrderComment &pVal, std::string &pStr)
{
    auto                     split = " ";
    std::vector<Item>        items;
    std::vector<std::string> item_comments;
    std::vector<float>       marks;

    std::vector<std::string> tokens;
    std::istringstream       iss(split);
    std::string              token;

    auto i = 0;
    while(iss >> token)
    {
        tokens.push_back(token);
        std::istringstream       iss_token("*");
        std::vector<std::string> part;
        std::string              tk;
        Item                     it;
        while(iss_token >> tk)
        {
            part.push_back(tk);
        }
        it.set_name(part[0]);
        items.push_back(it);
        marks.push_back(std::stof(part[1]));
        item_comments.push_back(part[2]);
    }
}

std::string Convert::OrderComment2String(std::string &pstr)
{
    return {};
}
